<style rel="stylesheet/scss" lang="scss" scoped>
</style>

<template>
    <div class="container">
        <el-card shadow="hover" style="padding:0">
            
            <!-- 操作按钮 -->
            <el-card class="operate_container" shadow="never">
                <div class="wrapper">
                    <div><i class="el-icon-tickets" style="margin-right: 10px;"></i>数据列表</div>
                    <div>
                        <el-button type="primary" icon="el-icon-plus" size="medium" @click="addOrUpdateVisible">添加</el-button>
                        <el-button type="danger" icon="el-icon-delete" size="medium" @click="handleDelete">删除</el-button>
                        <el-button type="primary" icon="el-icon-refresh" size="medium" @click="handleResetSearch('searchForm')">刷新</el-button>
                        <el-button type="primary" icon="el-icon-search" size="medium" @click="condition = !condition">过滤</el-button>
                    </div>
                </div>
            </el-card>

            <!-- 条件过滤 -->
            <el-card class="filter_container" shadow="never" v-show="condition">
                <div>
                    <span><i class="el-icon-search" style="margin-right: 10px;"></i>筛选搜索</span>
                    <el-button style="float:right" type="primary" @click="handleSearch()" size="medium">搜索</el-button>
                    <el-button style="float:right;margin-right: 15px" @click="handleResetSearch('searchForm')" size="medium">重置</el-button>
                </div>
                <div style="margin-top: 15px">
                    <el-form ref="searchForm" :inline="true" :model="searchForm" size="medium" label-width="120px">
                        <el-form-item label="用户ID" prop="userId">
	                        <el-input v-model="searchForm.userId" clearable placeholder="用户ID"></el-input>
	                    </el-form-item>
                        <el-form-item label="角色ID" prop="roleId">
	                        <el-input v-model="searchForm.roleId" clearable placeholder="角色ID"></el-input>
	                    </el-form-item>
                    </el-form>
                </div>
            </el-card>

            <!-- 表格 -->
            <el-table
                ref="table" size="small" empty-text="暂无数据"
                element-loading-text="给我一点时间"
                fit stripe border highlight-current-row
                v-loading="dataListLoading"
                :data="dataList"
                :header-cell-style="{'padding-top': '10px', 'height': '50px', 'color': '#515A6E', 'background-color': '#f8f8f9'}"
                @selection-change="handleSelection">
                <el-table-column type="selection" width="55" align="center"></el-table-column>
                <el-table-column type="index" width="55" align="center"></el-table-column>
                
                <el-table-column label="用户ID" prop="userId" align="center"/>
                <el-table-column label="角色ID" prop="roleId" align="center"/>
                
                <!--
                <el-table-column label="状态" width="80px" align="center">
                    <template slot-scope="scope">
                        <el-tag type="success" size="small" v-if="scope.row.status === true">正常</el-tag>
                        <el-tag type="danger" size="small" v-else>禁用</el-tag>
                    </template>
                </el-table-column>
                -->
                
                
                <el-table-column label="操作" width="120px" align="center" fixed="right">
                    <template slot-scope="scope">
                        <div class="table_action">
                            <el-button type="primary" icon="el-icon-edit" circle size="small" @click="addOrUpdateVisible(scope.row)"></el-button>
                            <el-button type="danger" icon="el-icon-delete" circle size="small" @click="handleDelete(scope.row)"></el-button>
                            
                            <!--
                            <el-button type="text" size="small" @click="handleOpenDialog('edit', scope.row)">编辑</el-button>
                            <el-button type="text" size="small" @click="handleDelete(scope.row)">删除</el-button>
                            
                            <el-popover
                                class="table_popover"
                                placement="bottom"
                                trigger="hover">
                                <div class="table_popover_tools">
                                    <el-button type="text" size="small">编辑</el-button>
                                    <el-button type="text" size="small">删除</el-button>
                                </div>
                                <el-button icon="el-icon-s-tools" circle slot="reference" size="small"></el-button>
                            </el-popover>

                            <div class="table_action_dropdown">
                                <el-dropdown split-button type="primary" size="small" style="margin-left: 10px;">
									操作
                                    <el-dropdown-menu slot="dropdown">
                                        <el-dropdown-item>黄金糕</el-dropdown-item>
                                        <el-dropdown-item>狮子头</el-dropdown-item>
                                        <el-dropdown-item>螺蛳粉</el-dropdown-item>
                                        <el-dropdown-item>双皮奶</el-dropdown-item>
                                        <el-dropdown-item>蚵仔煎</el-dropdown-item>
                                    </el-dropdown-menu>
                                </el-dropdown>
                            </div>

                            <div class="table_action_dropdown margin-left-10">
                                <el-dropdown>
                                    <span class="el-dropdown-link">
										下拉菜单<i class="el-icon-arrow-down el-icon--right"></i>
                                    </span>
                                    <el-dropdown-menu slot="dropdown">
                                        <el-dropdown-item>黄金糕</el-dropdown-item>
                                        <el-dropdown-item>狮子头</el-dropdown-item>
                                        <el-dropdown-item>螺蛳粉</el-dropdown-item>
                                        <el-dropdown-item disabled>双皮奶</el-dropdown-item>
                                        <el-dropdown-item divided>蚵仔煎</el-dropdown-item>
                                    </el-dropdown-menu>
                                </el-dropdown>
                            </div>
                            -->
                            
                        </div>
                    </template>
                </el-table-column>
            </el-table>

            <!-- 分页工具栏 -->
            <div class="pagination">
                <el-pagination
                        @size-change="handleSizeChange"
                        @current-change="handleCurrentChange"
                        :current-page="searchForm.page"
                        :page-sizes="[10, 20, 30, 50, 100, 200]"
                        :page-size="searchForm.limit"
                        layout="total, sizes, prev, pager, next, jumper"
                        :total="totalPage">
                </el-pagination>
            </div>

            <!-- 窗口 -->
            <el-dialog
                :title="!form.id ? '新增' : '修改'"
                :visible.sync="visible" :close-on-click-modal="false" @close="handleDialogClose">
                <el-form ref="form" :model="form" :rules="rules" label-width="120px">
                	
                    <el-form-item label="用户ID" prop="userId">
                        <el-input v-model="form.userId" placeholder="用户ID" style="width: 80%;"></el-input>
                    </el-form-item>
                    <el-form-item label="角色ID" prop="roleId">
                        <el-input v-model="form.roleId" placeholder="角色ID" style="width: 80%;"></el-input>
                    </el-form-item>
                    
                    <!--
                    <el-select style="width: 100%;" v-model="form.sex" placeholder="员工性别">
                        <el-option label="男" :value="1"></el-option>
                        <el-option label="女" :value="2"></el-option>
                    </el-select>
                    <el-form-item label="排序" prop="sort">
                        <el-input-number v-model="form.sort" :min="1" :max="10000" placeholder="排序"></el-input-number>
                    </el-form-item>
                    <el-form-item label="备注" prop="remarks">
                        <el-input type="textarea" :rows="2" placeholder="请输入内容" style="width: 80%;" v-model="form.remarks"></el-input>
                    </el-form-item>
                    <el-form-item label="状态" prop="status">
                        <el-radio-group v-model="form.status">
                            <el-radio :label="true" border>正常</el-radio>
                            <el-radio :label="false" border>锁定</el-radio>
                        </el-radio-group>
                    </el-form-item>
                    <el-form-item label="服务保证" prop="status">
                       <el-radio-group v-model="form.promotionType" size="small">
                            <el-radio-button :label="0">无优惠</el-radio-button>
                            <el-radio-button :label="1">特惠促销</el-radio-button>
                            <el-radio-button :label="2">会员价格</el-radio-button>
                            <el-radio-button :label="3">阶梯价格</el-radio-button>
                            <el-radio-button :label="4">满减价格</el-radio-button>
                        </el-radio-group>
                    </el-form-item>
                    -->
                    
                </el-form>

                <div slot="footer" class="dialog-footer">
                    <el-button @click="visible = false">取 消</el-button>
                    <el-button @click="handleSubmit('form')" type="primary" :loading="submitLoading">提 交</el-button>
                </div>
            </el-dialog>
        </el-card>
    </div>
</template>

<script>
import { save, deleted, updated, info, list, page, selectMyPage } from "@/api/admin/sysuserrole"

export default {
    data() {
        return {
            visible: false,
            condition: false,
            submitLoading: false,

            dataList: [],
            totalPage: 0,
            dataListLoading: true,

            searchForm: {
                page: 1,
                limit: 10,
                sortColumns: null,
                sortType: null,
                userId: null,
                roleId: null
            },
            form: {
                id: null,
                userId: null,
                roleId: null
            },
            rules: {
                userId: [
                    { required: true, message: '用户ID不能为空', trigger: 'blur' }
                ],
                roleId: [
                    { required: true, message: '角色ID不能为空', trigger: 'blur' }
                ]
            }
        }
    },
    mounted() {
    	this.$nextTick(function () {
	        this.getDataList()
    	})
    },
    methods: {
        handleSelection(selection) { // 多选
            this.selectionRows = selection
        },
        handleSizeChange(val) { // 每页数量
            this.searchForm.limit = val
            this.getDataList()
        },
        handleCurrentChange(val) { // 页码
            this.searchForm.page = val
            this.getDataList()
        },
        handleResetSearch(name) { // 重置搜索表单
            this.handleReset(name)
            this.getDataList()
        },
        handleReset(name) { // 重置表单
            const refs = this.$refs
            refs[name].resetFields()
        },
        handleSearch() { // 单条件搜索
            this.getDataList()
        },
        handleDialogClose() { //表单窗口关闭
            this.handleReset('form')
        },

		/**
         * 打开表单窗口
         */
		addOrUpdateVisible(row) {
            this.visible = true
            if (row.id) {
                this.getInfo(row.id).then(res => {
                    this.form = Object.assign({}, res.data)
                })
            } else {
                this.form.id = null
            }
        },

		/**
         * 提交表单
         */
        handleSubmit(name) {
        	const refs = this.$refs
            refs[name].validate((valid) => {
                if (valid) {
                    this.submitLoading = true
                    if (!this.form.id) {
                        save(this.form).then(res => {
                            if (this.ResultMessage(res)) {
                                this.submitLoading = false
                                this.visible = false
                                this.handleReset('form')
                                this.getDataList()
                            } else {
                                this.submitLoading = false
                            }
                        }).catch(error => {
                            this.submitLoading = false
                            this.$notify({ title: '错误', message: error, type: 'error' });
                        })
                    } else if (this.form.id) {
                        updated(this.form).then(res => {
                            if (this.ResultMessage(res)) {
                                this.submitLoading = false
                                this.visible = false
                                this.handleReset('form')
                                this.getDataList()
                            } else {
                                this.submitLoading = false
                            }
                        }).catch(error => {
                            this.submitLoading = false
                            this.$notify({ title: '错误', message: error, type: 'error' });
                        })
                    } else {
                        console.log('未知操作');
                    }
                }
            })
        },
		
		/**
         * 删除
         */
        handleDelete(row) { 
            let ids = []
            if (row.id != undefined) {
                ids.push(row.id)
            }
            if(this.selectionRows) {
                this.selectionRows.filter((item, index) => {
                    ids.push(item.id)
                })
            }
            if (ids.length <= 0) {
                this.$notify({ title: '提示', message: '请选择需要删除的记录！', type: 'info' });
            } else {    
                this.$confirm('确定要删除？', '提示', { confirmButtonText: '确定', cancelButtonText: '取消', type: 'warning' }).then(() => {
                    deleted({ idList: ids }).then(res => {
                        if (this.ResultMessage(res)) {
                            this.getDataList()
                        } 
                    }).catch(error => {
                        this.$notify({ title: '错误', message: error, type: 'error' });
                    })
                }).catch(() => {})
            }
        },

		/**
         * 详细信息
         */
        getInfo(id) {
            if (id !== undefined) {
                return info(id).then(res => {
                    if (this.ResultMessage(res, false)) {
                        return res
                    } else {
                        this.$notify({ title: '错误', message: error, type: 'error' });
                    }
                }).catch(error => {
                    this.$notify({ title: '错误', message: error, type: 'error' });
                })
            }
        },
		
        /**
         * 数据列表
         */
        getDataList() {
            page(this.searchForm).then(res => {
                if (this.ResultMessage(res, false)) {
                    this.dataList = res.data.records
                    this.totalPage = res.data.total
                    this.dataListLoading = false
                } else {
                    this.dataListLoading = false
                }
            }).catch(error => {
                this.dataListLoading = false
                this.$notify({ title: '错误', message: error, type: 'error' });
            })
        }
    }
}
</script>